package com.puyuba.puyuba.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.bean.LevelInfoBean;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;

import com.pyb.java.network.scheduler.RxSchedulers;
import com.puyuba.puyuba.network.UserApi;
import com.pyb.java.commonutil.ToastHelper;
import com.puyuba.puyuba.view.adapter.FormAdatper;
import com.puyuba.puyuba.view.adapter.LevelAdatper;

import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;

/**
 * 筛选条件dialog
 */

public class FilterDialog extends Dialog {

    private Button btnSure;
    private RecyclerView recycleviewForm;
    private Button closeBn;
    private RecyclerView recyclerView;
    private LevelAdatper levelAdapter;
    private RelativeLayout rlBg;
    private OnClickBottomListener listener;
    private FormAdatper formAdatper;
    private int formposition = 0;//学习形式选择position
    private int levelposition = 0;//学习层次选择position
    private List<LevelInfoBean> getdata;


    public FilterDialog(Context context, OnClickBottomListener listener) {
        super(context, R.style.ContactDialog);
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filter_dialog_layout);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        initView();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {

        initData();

        closeBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rlBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                listener.onFilterClick(getdata.get(formposition).getId(),getdata.get(formposition).getTo().get(levelposition).getId());
            }
        });

    }

    private void initView() {
        btnSure = (Button) findViewById(R.id.btn_sure);
        closeBn = (Button) findViewById(R.id.btn_close);
        recyclerView = (RecyclerView) findViewById(R.id.recycleview_level);
        rlBg = (RelativeLayout) findViewById(R.id.rl_bg);
        recycleviewForm = (RecyclerView) findViewById(R.id.recycleview_form);

    }

    public interface OnClickBottomListener{
        public void onFilterClick(String formid,int levelid);
    }

    private void initData(){
        RetrofitManger.getInstance().createService(UserApi.class)
                .getcategorylist()
                .compose(RxSchedulers.<ResponseEntity<List<LevelInfoBean>>>applySchedulers())
                .subscribe(new ResponseObserve<List<LevelInfoBean>>() {

                    @Override
                    public void onSuccess(ResponseEntity<List<LevelInfoBean>> responseEntity) {
                        getdata = responseEntity.getData();
                        getdata.get(formposition).setSelect(true);//默认选中第一个
                        formAdatper = new FormAdatper(getContext(), getdata);
                        GridLayoutManager manager = new GridLayoutManager(getContext(),3);
                        manager.setOrientation(GridLayoutManager.VERTICAL);

                        recycleviewForm.setLayoutManager(manager);
                        recycleviewForm.setAdapter(formAdatper);

                        formAdatper.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                switch (view.getId()) {
                                    case R.id.btn_recycleview_level:
                                        formposition = position;
                                        levelposition = 0;

                                        //学习形式状态改变
                                        for (int a = 0; a < getdata.size(); a++){
                                            if (a == formposition){
                                                getdata.get(a).setSelect(true);
                                            }else {
                                                getdata.get(a).setSelect(false);
                                                //学习层次状态清空
                                                for (int b = 0; b < getdata.get(a).getTo().size(); b++){
                                                    getdata.get(a).getTo().get(b).setSelect(false);
                                                }
                                            }
                                        }
                                        //每次切换学习形式，清空学习层次状态，并默认选中第一个
                                        getdata.get(formposition).getTo().get(levelposition).setSelect(true);
                                        formAdatper.notifyDataSetChanged();
                                        levelAdapter.setNewData(getdata.get(formposition).getTo());
                                        levelAdapter.notifyDataSetChanged();
                                        break;
                                }
                            }
                        });
                        formAdatper.notifyDataSetChanged();
                        getdata.get(0).getTo().get(0).setSelect(true);//默认选中第一个
                        levelAdapter = new LevelAdatper(getContext(), getdata.get(formposition).getTo());
                        GridLayoutManager manager2 = new GridLayoutManager(getContext(),3);
                        manager2.setOrientation(GridLayoutManager.VERTICAL);

                        recyclerView.setLayoutManager(manager2);
                        recyclerView.setAdapter(levelAdapter);

                        levelAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                            @Override
                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                switch (view.getId()) {
                                    case R.id.btn_recycleview_level:
                                        levelposition = position;
                                        for (int a = 0; a < getdata.get(formposition).getTo().size(); a++){
                                            getdata.get(formposition).getTo().get(a).setSelect(a == levelposition ? true : false);
                                        }
                                        levelAdapter.notifyDataSetChanged();
                                        break;
                                }
                            }
                        });
                        levelAdapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onFail(String errorMsg) {
                        if (errorMsg != null&&!errorMsg.equals("")){
                            ToastHelper.show(errorMsg);
                        }else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@io.reactivex.annotations.NonNull Disposable disposable) {
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }

}